package com.d3works.apps.supplychain.service;

import com.d3works.apps.base.db.Company;
import com.d3works.apps.sale.db.SaleOrder;
import com.d3works.apps.sale.db.SaleOrderLine;
import com.d3works.apps.stock.db.StockMove;
import com.d3works.apps.stock.db.StockMoveLine;
import com.d3works.exception.D3worksException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface SaleOrderStockService {

  /**
   * Create a delivery stock move from a sale order.
   *
   * @param saleOrder
   * @return
   * @throws D3worksException
   */
  public List<Long> createStocksMovesFromSaleOrder(SaleOrder saleOrder) throws D3worksException;

  public StockMove createStockMove(
      SaleOrder saleOrder, Company company, LocalDate estimatedDeliveryDate)
      throws D3worksException;

  public StockMoveLine createStockMoveLine(StockMove stockMove, SaleOrderLine saleOrderLine)
      throws D3worksException;

  public StockMoveLine createStockMoveLine(
      StockMove stockMove, SaleOrderLine saleOrderLine, BigDecimal qty) throws D3worksException;

  public boolean isStockMoveProduct(SaleOrderLine saleOrderLine) throws D3worksException;

  boolean isStockMoveProduct(SaleOrderLine saleOrderLine, SaleOrder saleOrder)
      throws D3worksException;

  /**
   * Update delivery state by checking delivery states on the sale order lines.
   *
   * @param saleOrder
   */
  void updateDeliveryState(SaleOrder saleOrder) throws D3worksException;

  /**
   * Update delivery states in sale order and sale order lines.
   *
   * @param saleOrder
   * @throws D3worksException
   */
  void fullyUpdateDeliveryState(SaleOrder saleOrder) throws D3worksException;

  /**
   * Find the sale order linked to the stock move.
   *
   * @param stockMove a stock move
   * @return the found sale order, or an empty optional if no sale order was found.
   */
  Optional<SaleOrder> findSaleOrder(StockMove stockMove);
}
